<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;
use app\common\library\EasyWechat;
use think\cache\driver\Redis;
use think\facade\Config;
use think\facade\Cache;
use app\api\common\controller\LuckDraw;
use think\facade\Db;
use think\facade\Request;
use app\api\model\Yjcomm;

/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Lease extends Api
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
    public function __construct()
    {
        
        $this->Yjcomm = new Yjcomm();
    }
  public  function get_weeks($time = '', $format='m-d')
    {
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        for ($i=0; $i<=6; $i++) {
            // $date[$i] = date($format, strtotime('+' . $i-7 .' days', $time));
            // $day=date($format, strtotime('+' . $i-7 .' days', $time));
            $day=date($format, strtotime('+' . $i .' days', $time));
            // $date[$i][$this->get_week($day)]["first"] =$day." 00:00:00";
            // $date[$i][$this->get_week($day)]["last"] = $day." 23:59:59";
  
            $date[$i]['id'] =strtotime('+' . $i .' days', $time);
            $date[$i]['item'] = $day;
        }
        $array = array("日","一","二","三","四","五","六");
         foreach ($date as $k => $v) {
          
           
           if(date('d',$v['id'])==date('d')){
              $date[$k]['week']="今天";
              $date[$k]['checked']=true;
           }elseif(date('d',$v['id'])==date('d')+1){
               $date[$k]['week']="明天";
               $date[$k]['checked']=false;
           }else{
                $date[$k]['week']="周".$array[date('w',$v['id'])];
                $date[$k]['checked']=false;
           }
        }
        return $date;
    }
    public function get_week($date){
        $date_str=date('Y-m-d',strtotime($date));
        
        $arr=explode("-", $date_str);
        
        $year=$arr[0];
        
        $month=sprintf('%02d',$arr[1]);
        
        $day=sprintf('%02d',$arr[2]);
        
        $hour = $minute = $second = 0;
        
        $strap = mktime($hour,$minute,$second,$month,$day,$year);
        
        $number_wk=date("w",$strap);
        
        $weekArr=array("周日","周一","周二","周三","周四","周五","周六");
       
        return $weekArr[$number_wk];
        
        }
    //租赁列表
     public function getLeaseLists(){
        $keywords=input('keywords');
        $where=[];
        if($keywords)  $where=[
            ['title','like','%'.$keywords.'%']
        ];
        $data=Db::name('lease')->field('id,title,thumb,type,price,activity_price,deposit,spec_type,specs,specs_value,week_state,unit')->where($where)->select()->toArray();
        foreach ($data as $k => $v) {
            if($v['spec_type']=="more"){
                    $spec=$this->getSpecItem($v,$v['id']);
                     $data[$k]['childern']=$spec;
                     $lease_price=$this->getCheckPrice($spec,$v['id']);
                     if($lease_price){
                        $data[$k]['price']=$lease_price['price'];
                        $data[$k]['activity_price']=$lease_price['activity_price'];
                        // if($this->Yjcomm->getDataDay()){//节假日
                        
                        //     $data[$k]['activity_price']=$lease_price['price'];
                        // }
                    }
            }else{
                if($this->Yjcomm->getDataDay()){//节假日
                            $data[$k]['activity_price']=$v['price'];
                        }
            }
            $data[$k]['number']=1;
           
        }
        $this->result('success',$data,200,'json');
     }
     // 详情
    public function getLeaseDetail(){
        
        $Lease_id=input('lease_id');
        if(!$Lease_id)$this->result('lease_id参数错误',[],403,'json');
        $data=Db::name('lease')->field('id,title,thumb,type,price,activity_price,deposit,spec_type,specs,specs_value,week_state,unit,main_image,detail_image')->where('id',$Lease_id)->find();
        if($data['main_image']) $data['main_image']=explode('|',$data['main_image']);
        if($data['detail_image']) $data['detail_image']=explode('|',$data['detail_image']);
        $data['server_type']=2;
        if($data['spec_type']=="more"){
           
            $spec=$this->getSpecItem($data,$Lease_id);

            $lease_price=$this->getCheckPrice($spec,$Lease_id);
            $data['spec']=$spec;
       
           if($lease_price){
                $data['price']=$lease_price['price'];
                $data['activity_price']=$lease_price['activity_price'];
                 // if($this->Yjcomm->getDataDay()){//节假日
                            // $data['activity_price']=$lease_price['price'];
                    // }
           }
            
        }else{
                if($this->Yjcomm->getDataDay()){//节假日
                            $data[$k]['activity_price']=$data['price'];
                        }
            }
        
         $this->result('success',$data,200,'json');
       
    } 
    public function getCheckPrice($spec,$Lease_id){
        
         $list=[];
            $check_val=[];
            $day_time=date('w');
            foreach ($spec as $k => $v) {
                foreach ($v['childern'] as $key => $value) {
                    if($value['checked']==true){
                        if($value['id']>100){
                            $day_time=date('w',$value['id']);
                            $spec_id=Db::name('spec_item')->where('spec_id',2)->where('item',$day_time)->value('id');
                            array_push($check_val,$spec_id);
                        }else{
                            array_push($check_val,$value['id']);
                        }
                        
                    }
                }
                
            }
          
            sort($check_val);
            $check_val=implode('_',$check_val);

            $where=[
                ['lease_id','=',$Lease_id],
                ['sku_val','=',$check_val],
            ];
            if($Lease_id==1){
                array_push($where,['day_time','=',$day_time]);
            }
            $lease_price=Db::name('lease_price')->where($where)->fetchsql(false)->find();
         
           if($lease_price){
                $list['price']=$lease_price['price'];
                $list['activity_price']=$lease_price['activity_price'];
           }
           return $list;
    }
    public function getSpecItem($data,$Lease_id){
             $specs_value=json_decode($data['specs_value'],true);
            $spec='';
         
             $spec=Db::name('spec')->where('type',1)->whereIn('id',$data['specs'])->order('sort asc')->select()->toArray();
              $product_value=Db::name('lease_price')->where(['lease_id'=>$Lease_id])->column('sku_val');
                $temp_sku=[];
                foreach ($product_value as $k => $v) {
                        $sku=explode('_',$v);
                        foreach ($sku as $key => $value) {
                            if(!empty($temp_sku)){
                                if(!in_array($value,$temp_sku))  array_push($temp_sku,$value);
                            } else{
                                array_push($temp_sku,$value);
                            }  
                           
                        }
                }
                    foreach ($spec as $k => $v) {
                        if($v['id']!=2){
                            $where_spec=[
                                ['spec_id','=',$v['id']],
                                ['id','in',$temp_sku],
                                ['item','in',$specs_value[$v['title']]]
                            ];
                            // if(in_array(date('w'),[6,0]) && $data['week_state']==0){//针对周六日只展示全天和过夜可以租赁
                            //     array_push($where_spec, ['id','not in',[1,2]]);
                            // }
                            $childern=Db::name('spec_item')->field('id,item')->where($where_spec)->order('id asc')->select()->toArray();
                            foreach ($childern as $k1 => $v1) {
                                if($k1==0){
                                    
                                    $childern[$k1]['checked']=true;
                                }else{
                                    $childern[$k1]['checked']=false;
                                }
                            }
                            $spec[$k]['childern']=$childern;

                        }else{
                            $spec[$k]['childern']=$this->get_weeks();
                        }
                    
                       
                    }
        return $spec;
    }
    //树屋编号
    public function getHouseNumber(){

        $post=Request::param();
        $house_id=0;
        $order_id=[];
        $wherserver=[];
        if(isset($post['spec_id'])){
           $spec_id=$post['spec_id'];//1681110252,2
           $spec_id=explode(',',$spec_id);
           // $house_id=$post['house_id'];
           $lease_id=$post['lease_id'];
           sort($spec_id);
           $start_time=strtotime(date('Y-m-d 00:00:00',$spec_id[1]));
           $end_time=strtotime(date('Y-m-d 23:59:59',$spec_id[1]));
           $where=[
            ['use_time','between',[$start_time,$end_time]], 
            ['orders_status','in',[1,2]]
           ];
           $order_id=Db::name("order")->where($where)->fetchsql(false)->column('id');
          
           $wherserver=[
           
             ['order_id','in',$order_id],
             ['server_id','=',$lease_id],
             ['type','=',2],
           ];
           $order=Db::name('order_server')->where($wherserver)->where('time_id','not null')->column('time_id');
         
           $count=0;
           if($order){
                foreach ($order as $k => $v) {
                   if($v==$spec_id[0]){
                        $count+=1;
                   }elseif(($v==4 || $v==14) && (in_array($spec_id[0],[1,2]))){
                      $count+=1;
                   }elseif(($spec_id[0]==4 || $spec_id[0]==14) && ($v==2 || $v==1 || $v==4 || $v==14)){

                        $count+=1;
                   }elseif(($spec_id[0]==14) && $v==3){

                        $count+=1;
                   }elseif(date('m',$spec_id[1])==date('m') && date('H')>=14 && $spec_id[0]==1){
                    $count+=1;
                   }
               }
           
           }
        }
     
       $refund=Db::name('refund')->whereDay('created_at')->column('orders_id');
       $refund_house_id=[];
       if($refund && $order_id){
            $arr=array_intersect($order_id,$refund);
            $refund_house_id=Db::name('order_server')->whereIn('order_id',$arr)->where('house_num','not null')->column('house_num');//已退id
       }   
       $house_id=Db::name('order_server')->where($wherserver)->where('house_num','not null')->column('house_num');
       $data= Db::name('house')->select()->toArray();
       foreach ($data as $k => $v) {
            if(in_array($v['id'],$house_id) && $count>0){
                $data[$k]['checked']=false;
            }else{
                $data[$k]['checked']=true;
            }
            if(in_array($v['id'],$refund_house_id)){
                $data[$k]['checked']=true;
            }
       }
        $this->result('success',$data,200,'json');
    }

    //选择切换价格
    public function getRelpacePrice(){
        $spec_id=input('spec_id');
        $lease_id=input('lease_id');
        $spec_id=explode(',',$spec_id);
        sort($spec_id);
        $whe_spec=[];
        $spec_item=0;
        if(count($spec_id)==2){
                $spec_item=Db::name('spec_item')->where('spec_id',2)->where('item',date('w',$spec_id[1]))->value('id');
        }
        $temp=[];
        array_push($temp,$spec_id[0]);
        if($spec_item>0){
            array_push($temp,$spec_item);
        }
        sort($temp);
        $temp=implode('_',$temp);
        $where=[
            ['sku_val','=',$temp],
            ['lease_id','=',$lease_id]
        ];
        $data=Db::name('lease_price')->field('price,activity_price')->where($where)->find();
         if($this->Yjcomm->getDataDay()){//节假日
                            $data['activity_price']=$data['activity_price'];
                        }
        $this->result('success',$data,200,'json');
    }
    public function getOtherRelpacePrice(){
         $lease_id=input('lease_id');
         $time=input('time');
          $where=[
            ['id','=',$lease_id]
        ];
        $data=Db::name('lease')->field('price,activity_price')->where($where)->find();
         $year = date('Y', strtotime($time));
        $dayOfWeek = date('N', strtotime($time));
                $holiday = array(
                    date('Y-m-d', strtotime("$year-01-01")), //元旦
                    date('Y-m-d', strtotime("$year-04-05")), //清明节
                    date('Y-m-d', strtotime("$year-04-29")), //劳动节
                    date('Y-m-d', strtotime("$year-04-30")), //劳动节
                    date('Y-m-d', strtotime("$year-05-01")), //劳动节
                    date('Y-m-d', strtotime("$year-05-02")), //劳动节
                    date('Y-m-d', strtotime("$year-05-03")), //劳动节
                    date('Y-m-d', strtotime("$year-06-01")), //儿童节
                    date('Y-m-d', strtotime("$year-06-22")), //端午节
                    date('Y-m-d', strtotime("$year-06-23")), //端午节
                    date('Y-m-d', strtotime("$year-06-24")), //端午节
                    date('Y-m-d', strtotime("$year-09-29")), //国庆节
                    date('Y-m-d', strtotime("$year-09-30")), //国庆节
                    date('Y-m-d', strtotime("$year-10-01")), //国庆节
                    date('Y-m-d', strtotime("$year-10-02")), //国庆节
                    date('Y-m-d', strtotime("$year-10-03")), //国庆节
                    date('Y-m-d', strtotime("$year-10-04")), //国庆节
                    date('Y-m-d', strtotime("$year-10-05")), //国庆节
                    date('Y-m-d', strtotime("$year-10-06")), //国庆节
                   
                );
        if($dayOfWeek == 6 || $dayOfWeek == 7 || in_array($year,$holiday)){
            $data['activity_price']=$data['price'];
        }
        $this->result('success',$data,200,'json');
    }

}